package view;

import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseListener;
import java.awt.Event;
import java.awt.BorderLayout;

import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.KeyStroke;
import java.awt.Point;

import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JFrame;
import javax.swing.JDialog;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.GridBagLayout;
import java.io.File;

import javax.swing.JSlider;
import javax.swing.JButton;
import javax.swing.filechooser.FileNameExtensionFilter;

import draw.objviewer.RenderMan;
import javax.swing.JTree;
import java.awt.GridBagConstraints;
import javax.swing.JScrollBar;


public class EZ_CITY {

	private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="10,10"
	private JPanel jContentPane = null;
	private JMenuBar jJMenuBar = null;
	private JMenu fileMenu = null;
	private JMenu editMenu = null;
	private JMenu helpMenu = null;
	private JMenuItem exitMenuItem = null;
	private JMenuItem aboutMenuItem = null;
	private JMenuItem cutMenuItem = null;
	private JMenuItem copyMenuItem = null;
	private JMenuItem pasteMenuItem = null;
	private JMenuItem saveMenuItem = null;
	private JMenuItem openMenuItem = null;
	private JDialog aboutDialog = null;
	private JPanel aboutContentPane = null;
	private JLabel aboutVersionLabel = null;
	private JPanel jPanel3d = null;
	private JPanel jPanelTime = null;
	private JSlider jSliderTime = null;
	private JPanel jPanelControls = null;
	private JPanel jPanelInfo = null;
	private JButton jButton1 = null;
	private JButton jButton2 = null;
	
	//opengl render
	public static RenderMan m_glr = null;  //  @jve:decl-index=0:
	private JButton jButton3 = null;
	private JTree jTreeSceneGraph = null;
	private JScrollBar jScrollBar = null;
	/**
	 * This method initializes jFrame
	 * 
	 * @return javax.swing.JFrame
	 */
	private JFrame getJFrame() {
		if (jFrame == null) {
			jFrame = new JFrame();
			jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			jFrame.setJMenuBar(getJJMenuBar());
			jFrame.setSize(1000, 597);
			jFrame.setContentPane(getJContentPane());
			jFrame.setTitle("Application");
		}
		return jFrame;
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJPanel3d(), null);
			jContentPane.add(getJPanelTime(), null);
			jContentPane.add(getJPanelControls(), null);
			jContentPane.add(getJPanelInfo(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jJMenuBar	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getFileMenu());
			jJMenuBar.add(getEditMenu());
			jJMenuBar.add(getHelpMenu());
		}
		return jJMenuBar;
	}



	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getFileMenu() {
		if (fileMenu == null) {
			fileMenu = new JMenu();
			fileMenu.setText("File");
			fileMenu.add(getOpenMenuItem());
			fileMenu.add(getSaveMenuItem());
			fileMenu.add(getExitMenuItem());
			
		}
		return fileMenu;
	}

	

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getEditMenu() {
		if (editMenu == null) {
			editMenu = new JMenu();
			editMenu.setText("Edit");
			editMenu.add(getCutMenuItem());
			editMenu.add(getCopyMenuItem());
			editMenu.add(getPasteMenuItem());
		}
		return editMenu;
	}

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getHelpMenu() {
		if (helpMenu == null) {
			helpMenu = new JMenu();
			helpMenu.setText("Help");
			helpMenu.add(getAboutMenuItem());
		}
		return helpMenu;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getExitMenuItem() {
		if (exitMenuItem == null) {
			exitMenuItem = new JMenuItem();
			exitMenuItem.setText("Exit");
			exitMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			});
		}
		return exitMenuItem;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getAboutMenuItem() {
		if (aboutMenuItem == null) {
			aboutMenuItem = new JMenuItem();
			aboutMenuItem.setText("About");
			aboutMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JDialog aboutDialog = getAboutDialog();
					aboutDialog.pack();
					Point loc = getJFrame().getLocation();
					loc.translate(20, 20);
					aboutDialog.setLocation(loc);
					aboutDialog.setVisible(true);
				}
			});
		}
		return aboutMenuItem;
	}

	/**
	 * This method initializes aboutDialog	
	 * 	
	 * @return javax.swing.JDialog
	 */
	private JDialog getAboutDialog() {
		if (aboutDialog == null) {
			aboutDialog = new JDialog(getJFrame(), true);
			aboutDialog.setTitle("About");
			aboutDialog.setContentPane(getAboutContentPane());
		}
		return aboutDialog;
	}

	/**
	 * This method initializes aboutContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getAboutContentPane() {
		if (aboutContentPane == null) {
			aboutContentPane = new JPanel();
			aboutContentPane.setLayout(new BorderLayout());
			aboutContentPane.add(getAboutVersionLabel(), BorderLayout.CENTER);
		}
		return aboutContentPane;
	}

	/**
	 * This method initializes aboutVersionLabel	
	 * 	
	 * @return javax.swing.JLabel	
	 */
	private JLabel getAboutVersionLabel() {
		if (aboutVersionLabel == null) {
			aboutVersionLabel = new JLabel();
			aboutVersionLabel.setText("Version 1.0");
			aboutVersionLabel.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return aboutVersionLabel;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getCutMenuItem() {
		if (cutMenuItem == null) {
			cutMenuItem = new JMenuItem();
			cutMenuItem.setText("Cut");
			cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
					Event.CTRL_MASK, true));
		}
		return cutMenuItem;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getCopyMenuItem() {
		if (copyMenuItem == null) {
			copyMenuItem = new JMenuItem();
			copyMenuItem.setText("Copy");
			copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
					Event.CTRL_MASK, true));
		}
		return copyMenuItem;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getPasteMenuItem() {
		if (pasteMenuItem == null) {
			pasteMenuItem = new JMenuItem();
			pasteMenuItem.setText("Paste");
			pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
					Event.CTRL_MASK, true));
		}
		return pasteMenuItem;
	}
	
	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getOpenMenuItem() {
		// TODO Auto-generated method stub
		
		
		if (openMenuItem == null) {
			openMenuItem = new JMenuItem();
			openMenuItem.setText("Open");
			openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
					Event.CTRL_MASK, true));
		}
		/*
		 * Open Dialog
		 */
		openMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				JFileChooser fileopen = new JFileChooser();
				FileNameExtensionFilter filter = new FileNameExtensionFilter(
						"OBJ files", "obj");
				fileopen.addChoosableFileFilter(filter);
				
				int returned = fileopen.showDialog(fileMenu, "Open file");

				if (returned == JFileChooser.APPROVE_OPTION) {
					File file = fileopen.getSelectedFile();
					// Adding the Model to load
					m_glr.addModel(file.toString());
				}
			}
		});
		return openMenuItem;
	
	}
	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getSaveMenuItem() {
		if (saveMenuItem == null) {
			saveMenuItem = new JMenuItem();
			saveMenuItem.setText("Save");
			saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
					Event.CTRL_MASK, true));
		}
		return saveMenuItem;
	}

	/**
	 * This method initializes jPanel3d	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel3d() {
		if (jPanel3d == null) {
			jPanel3d = new JPanel();
			jPanel3d.setSize(new Dimension(759, 509));
			jPanel3d.setLayout(new GridBagLayout());
			jPanel3d.setLocation(new Point(2, 2));
			jPanel3d.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
				public void mouseDragged(java.awt.event.MouseEvent e) {
					System.out.println("mouseDragged()"); // TODO Auto-generated Event stub mouseDragged()
				}
			});
			jPanel3d.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					System.out.println("mouseClicked()"); // TODO Auto-generated Event stub mouseClicked()
					
					
					
				}
			});
			jPanel3d.setSize(new Dimension(751, 511));
			
			
		}
		return jPanel3d;
	}

	/**
	 * This method initializes jPanelTime	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelTime() {
		if (jPanelTime == null) {
			jPanelTime = new JPanel();
			jPanelTime.setLayout(null);
			jPanelTime.setBounds(new Rectangle(1, 511, 761, 24));
			jPanelTime.add(getJSliderTime(), null);
		}
		return jPanelTime;
	}

	/**
	 * This method initializes jSliderTime	
	 * 	
	 * @return javax.swing.JSlider	
	 */
	private JSlider getJSliderTime() {
		if (jSliderTime == null) {
			jSliderTime = new JSlider();
			jSliderTime.setBounds(new Rectangle(1, 4, 760, 16));
		}
		return jSliderTime;
	}

	/**
	 * This method initializes jPanelControls	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelControls() {
		if (jPanelControls == null) {
			jPanelControls = new JPanel();
			jPanelControls.setLayout(null);
			jPanelControls.setBounds(new Rectangle(761, 2, 223, 313));
			jPanelControls.add(getJButton1(), null);
			jPanelControls.add(getJButton2(), null);
			jPanelControls.add(getJButton3(), null);
		}
		return jPanelControls;
	}

	/**
	 * This method initializes jPanelInfo	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelInfo() {
		if (jPanelInfo == null) {
			jPanelInfo = new JPanel();
			jPanelInfo.setLayout(null);
			jPanelInfo.setBounds(new Rectangle(761, 315, 222, 220));
			jPanelInfo.add(getJTreeSceneGraph(), null);
			jPanelInfo.add(getJScrollBar(), null);
		}
		return jPanelInfo;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(61, 96, 120, 26));
		}
		return jButton1;
	}

	/**
	 * This method initializes jButton2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setBounds(new Rectangle(64, 143, 118, 27));
		}
		return jButton2;
	}

	/**
	 * This method initializes jButton3	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton3() {
		if (jButton3 == null) {
			jButton3 = new JButton();
			jButton3.setBounds(new Rectangle(66, 196, 109, 26));
		}
		return jButton3;
	}

	/**
	 * This method initializes jTreeSceneGraph	
	 * 	
	 * @return javax.swing.JTree	
	 */
	private JTree getJTreeSceneGraph() {
		if (jTreeSceneGraph == null) {
			jTreeSceneGraph = new JTree();
			jTreeSceneGraph.setBounds(new Rectangle(2, 0, 200, 218));
		}
		return jTreeSceneGraph;
	}

	/**
	 * This method initializes jScrollBar	
	 * 	
	 * @return javax.swing.JScrollBar	
	 */
	private JScrollBar getJScrollBar() {
		if (jScrollBar == null) {
			jScrollBar = new JScrollBar();
			jScrollBar.setBounds(new Rectangle(204, 0, 17, 219));
		}
		return jScrollBar;
	}

	/**
	 * Launches this application
	 */
	public static void main(String[] args) {
		
		
    	
		SwingUtilities.invokeLater(new Runnable() {
			
			public void run() {
				
				// setup OpenGL Version 2
		    	GLProfile profile = GLProfile.get(GLProfile.GL2);
		    	GLCapabilities capabilities = new GLCapabilities(profile);
		 
		    	// The canvas is the widget that's drawn in the JFrame
		    	GLCanvas glcanvas = new GLCanvas(capabilities);
		    	
		    	if(m_glr == null){
		    		
		    		m_glr = new RenderMan(glcanvas);
		    	}
		
				EZ_CITY application = new EZ_CITY();
				application.getJFrame().setVisible(true);
			    
				glcanvas.setSize(application.getJPanel3d().getWidth(), application.getJPanel3d().getHeight());
				application.getJPanel3d().add(glcanvas);
			    
				
				
			}
		});
	}

}
